package logmerger.main;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;


public class Driver {
	
	private static FTPClient ftpClient = new FTPClient();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		String username = "";
		String password = "";
		//String log_home = "/temp/logmerge";
		String filePrefix = "DeathClaim";
		String outputPath = "c:\\temp\\logMerge\\dc\\";
		String servers = "tahowas010:/usr/local/inet/logs/InttstCorpCell01/intDeathClaims1,tahowas020:/usr/local/inet/logs/InttstCorpCell01/intDeathClaims2";
		
		//FileMergeUtil.mergeFiles(outputPath, filePrefix);
		
		driver(username, password, filePrefix, outputPath, servers);
	}
	
	public static void driver(String username, String password, String filePrefix, String outputPath, String servers){
		try {
			String[] splitStr1 = servers.split(",");
			for(String split : splitStr1){
				String[] splitStr2 = split.split(":");
				ftpClient.setBufferSize(65536);
				connect(splitStr2[0]);
				login(username, EncodeDecodeUtil.decodeBase64(password));
				cwd(splitStr2[1]);
				pwd();
				setFileType(FTPClient.ASCII_FILE_TYPE);
				FTPFile[] files = listFiles(filePrefix);
				transferFiles(files, outputPath, splitStr2[0]);
				logout();
			}
			FileMergeUtil.mergeFiles(outputPath, filePrefix);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("DONE.  GOODBYE.");
	}
	
	private static FTPFile[] filterFiles(FTPFile[] files, String prefix){
		ArrayList<FTPFile> returnList = new ArrayList<FTPFile>();
		FTPFile[] returnArray = new FTPFile[0];
		
		for(FTPFile file : files){
			String fileName = file.getName();
			if(fileName.startsWith(prefix)){
				returnList.add(file);
			}
		}
		
		return returnList.toArray(returnArray);
	}
	
	private static void connect(String hostname) throws SocketException, IOException{
		ftpClient.connect(hostname);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
	}
	
	private static void login(String username, String password) throws IOException{
		boolean success = ftpClient.login(username, password);
		System.out.println("Login: " + success);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
	}
	
	private static void cwd(String path) throws IOException{
		boolean success = ftpClient.changeWorkingDirectory(path);
		System.out.println("CWD: " + success);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
	}
	
	private static String pwd() throws IOException{
		String result = ftpClient.printWorkingDirectory();
		System.out.println("PWD: " + result);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
		
		return result;
	}
	
	private static void setFileType(int fileType) throws IOException{
		boolean success = ftpClient.setFileType(fileType);
		System.out.println("Set Transfer Mode: " + success);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
	}
	
	private static void logout() throws IOException{
		boolean success = ftpClient.logout();
		System.out.println("Logout: " + success);
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
	}
	
	private static FTPFile[] listFiles(String filePrefix) throws IOException{
		FTPFile[] files = ftpClient.listFiles();
		String reply = ftpClient.getReplyString();
		System.out.println(reply);
		files = filterFiles(files, filePrefix);
		
		return files;
	}
	
	private static void transferFiles(FTPFile[] files, String outputPath, String serverName) throws IOException{
		for(FTPFile file : files){
			String fileName = file.getName();
			FileOutputStream fos = new FileOutputStream(outputPath + fileName + "." + serverName);
			boolean success = ftpClient.retrieveFile(fileName, fos);
			System.out.println("Retrieve File: " + success);
			String reply = ftpClient.getReplyString();
			System.out.println(reply);
			fos.close();
		}
		
	}
	
	

}
